package com.ruan.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruan.mapper.DepartLocationMapper;
import com.ruan.pojo.DepartLocation;
import com.ruan.service.DepartLocationService;
import com.ruan.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class DepartLocationServiceImpl extends ServiceImpl<DepartLocationMapper, DepartLocation> implements DepartLocationService {
    @Autowired
    DepartmentService departmentService;

    @Autowired
    DepartLocationService departLocationService;

    @Autowired
    DepartLocationMapper departLocationMapper;

    @Override
    public boolean updateDepartmentLocation(Long id,
                                            Double newLatitude,
                                            Double newLongitude) {
        LambdaQueryWrapper<DepartLocation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartLocation::getDepartmentId,id);
        DepartLocation departmentLocation = departLocationMapper.selectOne(queryWrapper);

        departmentLocation.setLatitude(String.valueOf(newLatitude));
        departmentLocation.setLongitude(String.valueOf(newLongitude));

        return departLocationService.updateById(departmentLocation);
    }

    @Override
    public boolean addDepartmentLocation(Long id,
                                            Double newLatitude,
                                            Double newLongitude) {
        DepartLocation departmentLocation = new DepartLocation();

        departmentLocation.setDepartmentId(id);
        departmentLocation.setLatitude(String.valueOf(newLatitude));
        departmentLocation.setLongitude(String.valueOf(newLongitude));

        return departLocationService.save(departmentLocation);
    }
}
